//
//  HZL_NearbyCollectionViewCell.m
//  B_Project
//
//  Created by 胡佳俊 on 15/12/1.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_NearbyCollectionViewCell.h"
#import "HZL_NearbyTableViewCell.h"
#import "Masonry.h"
#import "HZL_NetworkingHandle.h"
#import "HZL_NearbyModel.h"
#import "HZL_MapViewController.h"
@interface HZL_NearbyCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
// 刷新
@property (nonatomic, assign) NSInteger MJI;
@property (nonatomic, assign) BOOL isRefresh;
@property (nonatomic, retain) MJRefreshAutoStateFooter *footer;
@end
@implementation HZL_NearbyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArr = [NSMutableArray array];
        [self createCollectionView];
    }
    return self;
}
- (void)createCollectionView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height - 49) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 120;
    self.tableView.tableFooterView = [UIView new];
    [self.contentView addSubview:self.tableView];
    
    [self.tableView registerClass:[HZL_NearbyTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = YES;
        self.MJI = 0;
        [self getData];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        self.isRefresh = NO;
        self.MJI  = self.MJI + 20;
        [self getData];
    }];
    self.tableView.mj_footer = self.footer;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height - 49 - 35);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArr[indexPath.row];
    
    cell.block = ^(HZL_NearbyModel *model){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"要去吗?" message:model.address   preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                HZL_MapViewController *mVC = [[HZL_MapViewController alloc] init];
                mVC.latitude = model.location[@"lat"];
                mVC.longitude = model.location[@"lng"];
                mVC.add = model.address;
                mVC.name = model.name;
                UIResponder *responder = self.nextResponder.nextResponder.nextResponder;
                [((UIViewController *)responder).navigationController pushViewController:mVC animated:YES];
            }];
            [alert addAction:action1];
            [alert addAction:action2];
        
        UIResponder *responder = self.nextResponder.nextResponder.nextResponder;
        [((UIViewController *)responder) presentViewController:alert animated:YES completion:nil];
    };
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (void)setDic:(NSDictionary *)dic
{
    if ((_dic = dic)) {
        _dic = dic;
    }
    [self.tableView.mj_header beginRefreshing];
}

- (void)getData
{
    if (self.dic[@"category"]) {
        NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=%@&start=%ld&count=20&latitude=%@&longitude=%@", self.dic[@"category"], self.MJI,self.dic[@"latitude"], self.dic[@"longitude"]];
        NSLog(@"111%@", str);
        
        [HZL_NetworkingHandle getUrl:str cookie:nil result:^(id result) {
            
//            self.hud.hidden = YES;
//            if (!result) {
//                [self.contentView bringSubviewToFront:self.alertBtn];
//                return;
//            }
            
            // 下拉刷新
            if (self.isRefresh) {
                [self.dataArr removeAllObjects];
            }
            // 上拉加载
            
            if ([(NSArray *)result[@"items"] count] != 0) {
                [self.dataArr addObjectsFromArray:[HZL_NearbyModel transformWithArray:result[@"items"]]];
                [self.footer setTitle:@"点击或上拉加载" forState:MJRefreshStateIdle];
            }else {
                // 是否加载完
                [self.footer setTitle:@"已经到底了" forState:MJRefreshStateIdle];
            }
        
            [HZL_NetworkingHandle networkReachabilityStatus:^(id r) {
                if (![r isEqualToNumber:@0]) {
                    [self.tableView.mj_footer endRefreshing];
                }
            }];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
        }];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
